package main

import (
	"fmt"
	"math/rand"
	"time"
)

func main() {
	fmt.Println("课后练习1.1")
	arr := [5]string{"i", "am", "stupid", "and", "weak"}
	fmt.Println("修改前的数组为：", arr)
	arr = arrStringModify(arr)
	fmt.Println("修改后的数组为：", arr)
	fmt.Println("=====================")
	fmt.Println("课后联系1.2")
	var ch = make(chan int, 10)
	go producer(ch)
	go consumer(ch)
	for i := 0; i < 100; i++ {
		time.Sleep(time.Second)
		i++
	}
}

func arrStringModify(arr [5]string) [5]string {
	for i, val := range arr {
		if val == "stupid" {
			arr[i] = "smart"
		} else if val == "weak" {
			arr[i] = "strong"
		}
	}
	return arr
}

func producer(ch chan<- int) {
	defer close(ch)
	for true {
		a := rand.Intn(100)
		ch <- a
		time.Sleep(time.Second)
	}
}
func consumer(ch <-chan int) {
	for true {
		a := <-ch
		fmt.Println(a)
	}
}
